package com.ly.lypopupwindowdemo.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;


import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.activity.GetPicActivity;

import java.io.File;

/**
 * 调用系统的action,下载，市场，选择图片视频等
 * Created by Yhyu on 2015/5/29.
 */
public class IntentUtil {

    /**
     * 调用系统的下载
     *
     * @param context
     * @param url
     * @throws LException
     */
    public static void startSystemDownLoad(Context context, String url) throws LException {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LException(R.string.str_error_market);
        }
    }


    /**
     * 调用已安装的市场
     *
     * @param context
     * @throws LException
     */
    public static void startSystemMarket(Context context) throws LException {
        try {
            Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
            Intent marketIntent = new Intent(Intent.ACTION_VIEW, uri);
            marketIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(marketIntent);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LException(R.string.str_error_market);
        }
    }

    /**
     * 选择图片
     *
     * @param activity
     * @param requesCode
     * @throws LException
     */
    public static void startSystemCamera(Activity activity, int requesCode, Uri uri) throws LException {
        try {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
            activity.startActivityForResult(intent, requesCode);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LException(R.string.str_error_market);
        }
    }

    /**
     * 选择图片
     *
     * @param activity
     * @param requesCode
     * @throws LException
     */
    public static void startSystemSelectImage(Activity activity, int requesCode) throws LException {
        try {
            Intent intent = new Intent(Intent.ACTION_PICK, null);
            intent.setDataAndType(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "image/*");
            activity.startActivityForResult(intent, requesCode);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LException(R.string.str_error_market);
        }
    }

    /**
     * 选择视频
     *
     * @param activity
     * @param requesCode
     * @throws LException
     */
    public static void startSystemSelectVideo(Activity activity, int requesCode) throws LException {
        try {
            Intent intent = new Intent(Intent.ACTION_PICK, null);
            intent.setDataAndType(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "video/*");
            activity.startActivityForResult(intent, requesCode);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LException(R.string.str_error_market);
        }
    }

    /**
     * 解析选择的文件的路径，可能为null，若文件已不存在也返回null
     *
     * @param context
     * @param intent
     * @return
     */
    public static String getFilePathFromIntent(Context context, Intent intent) {
        Uri selectedImage = intent.getData();
        if (selectedImage == null) {
            return null;
        }
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = context.getContentResolver()
                .query(selectedImage, filePathColumn, null, null, null);
        String path;
        if (cursor != null && cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            path = cursor.getString(columnIndex);
            cursor.close();
        } else {
            path = selectedImage.toString().replace("file://", "");
        }
        if (TextUtils.isEmpty(path)) {
            return null;
        }
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        return path;
    }

    /**
     * 获取一张图片，返回结果为intent中的image_path
     *
     * @param activity
     * @param requestCode
     * @throws LException
     */
    public static void startGetpic(Activity activity, int requestCode) throws LException {
        try {
            Intent intent = new Intent(activity, GetPicActivity.class);
            intent.putExtra(GetPicActivity.IMAGE_CROP,false);
            activity.startActivityForResult(intent, requestCode);
        }catch (Exception e) {
            e.printStackTrace();
            throw new LException(R.string.str_error_market);
        }

    }
    /**
     * 获取一张图片，返回结果为intent中的image_path
     *
     * @param activity
     * @param requestCode
     * @throws LException
     */
    public static void startGetpicCrop(Activity activity, int requestCode) throws LException {
        try {
            Intent intent = new Intent(activity, GetPicActivity.class);
            intent.putExtra(GetPicActivity.IMAGE_CROP,true);
            activity.startActivityForResult(intent, requestCode);
        }catch (Exception e) {
            e.printStackTrace();
            throw new LException(R.string.str_error_market);
        }

    }

    /**
     * 打开系统裁剪图片，默认配置为圆 150*150
     *
     * @param activity
     * @param uri
     * @param requestCode
     * @throws LException
     */
    public static void startSystemCropImage(Activity activity, Uri uri, int requestCode) throws LException {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        startSystemCropImage(activity, intent, uri, requestCode);

    }

    /**
     * 打开系统裁剪图片，自定义配置缩放比例，intent配置，aspectX、aspectY、outputX、outputY
     *
     * @param activity
     * @param uri
     * @param requestCode
     * @throws LException
     */
    public static void startSystemCropImage(Activity activity, Intent intent, Uri uri, int requestCode) throws LException {
        try {
            intent.putExtra("crop", "true");
            intent.putExtra("return-data", true);
            intent.putExtra("scale", true);
            intent.putExtra("scaleUpIfNeeded", true);
            intent.setDataAndType(uri, "image/*");
            activity.startActivityForResult(intent, requestCode);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LException(R.string.str_error_market);
        }
    }


}
